namespace Sustainalytics.PdfGeneration.Service.Filters
{
    using System.Net;
    using System.Net.Http;
    using System.Web.Http.Filters;

    public static class HttpActionExecutionContextExtensions
    {
        public static HttpActionExecutedContext SetErrorResponseFor<T>(this HttpActionExecutedContext context, HttpStatusCode statusCode, string message = null)
        {
            if (context.Response == null && context.Exception is T)
            {
                context.Response = context.Request.CreateErrorResponse(statusCode, message ?? context.Exception.Message);
            }

            return context;
        }
    }
}